<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card style="margin-top/: 10px;">
      <el-row>
        <!-- 点击按钮,弹出卡片，添加数据 -->
        <el-col>
          <el-button type="primary" @click="cateDialogFn" style="margin-bottom: 10px;">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <!-- :data="获取过来渲染页面的数组" -->
      <el-table :data="cateList" style="width: 100%; margin-bottom: 20px" row-key="cat_id" border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <!--分类名称 -->
        <el-table-column prop="cat_name" label="分类名称" sortable width="800">
        </el-table-column>
        <!--是否有效 -->
        <el-table-column prop="cat_deleted" label="是否有效" sortable type:>
          <template v-slot="scope">
            <i class="el-icon-error" v-if="scope.row.cat_deleted == true" style="color:red"></i>
            <i class="el-icon-success" v-if="scope.row.cat_deleted == false" style="color:blue"></i>
          </template>
        </el-table-column>
        <!-- 排序 -->
        <el-table-column prop="cat_level" label="排序" width="280">
          <template v-slot="scope">
            <el-tag type="primary" v-if="scope.row.cat_level == 0">一级</el-tag>
            <el-tag type="success" v-if="scope.row.cat_level == 1">二级</el-tag>
            <el-tag type="warning" v-if="scope.row.cat_level == 2">三级</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="params.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="params.pagesize"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>

    </el-card>

    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="cateDialog" width="50%" @close="addCateClose">

      <!-- 添加分类的表单 -->
      <el-form :model="addFrom" :rules="addFromRules" ref="addFromRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input type="text" v-model="addFrom.cat_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="父级分类：">
          <!-- options 用来指定数据源 -->
          <!-- props 用来指定配置对象-->
          <el-cascader expand-trigger='hover' v-model="selectedKeys" :options="ParentList" :props="cascaderProps"
            @change="parentCateChanged" clearable change-on-select>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { goodsFenApi, postGoodsFenApi } from '../../api'
export default {
  data() {
    return {
      // 查询条件
      params: {
        type: 3,
        // 页码
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 5
      },
      // 商品分类数据
      cateList: [],
      // 父级分类数据
      ParentList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //选中的分级分类的id数组
      selectedKeys: [],
      // 总数据数量
      total: 0,
      // 控制添加分类对话框
      cateDialog: false,
      // 添加分类的表单数据对象
      addFrom: {
        // 将要添加的分类的名称
        cat_name: '',
        // 分级分类的id
        cat_pid: 0,
        // 默认要添加分类为1级
        cat_level: 0
      },
      addFromRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 编辑框的显示
      amenddialog: false,
      amendVal: '',
      amendId: -1,
      // 删除数据
      deletamenddialog: false,
      deletId: -1
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品数据
    async getCateList() {
      const res = await goodsFenApi(this.params)
      // console.log(this.params);
      console.log(res.data);
      this.cateList = res.data.data.result
      this.total = res.data.data.total
    },
    // 监听pagesize 改变
    handleSizeChange(newSize) {
      this.params.pagesize = newSize
      this.getCateList()
    },
    // 监听pagenum改变
    handleCurrentChange(newPage) {
      this.params.pagenum = newPage
      this.getCateList()
    },
    // 点击添加分类按钮
    cateDialogFn() {
      // 先获取父级数据列表 再展示对话框
      this.getParentList()
      this.cateDialog = true
    },
    // 获取父级分类的数据列表
    async getParentList() {
      const { data: res } = await goodsFenApi(this.params)
      if (res.meta.status !== 200) {
        return this.$message.error('获取父类数据失败')
      }
      // console.log(res)
      this.ParentList = res.data.result
      // console.log(this.ParentList)
    },
    // 选中项发生变化出发此函数
    parentCateChanged() {
      // console.log(this.selectedKeys)
      // 如果selectedKeys数组中的length 大于0 证明选中了父级分类  如果等于0  就说明没有选中父级分类
      if (this.selectedKeys.length > 0) {
        // 父级分类的Id
        this.addFrom.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 为当前分类的等级赋值
        this.addFrom.cat_level = this.selectedKeys.length
        return
      } else {
        // 父级分类的Id
        this.addFrom.cat_pid = 0
        // 为当前分类的等级赋值
        this.addFrom.cat_level = 0
      }
    },
    // 确定添加分类
    addCate() {
      // console.log(this.addFrom.cat_name)
      if (!this.addFrom.cat_name.trim()) {
        return alert('请输入商品名称')
      }
      // console.log(this.addFrom)
      this.$refs.addFromRef.validate(async (valid) => {
        if (!valid) return


        const res = await postGoodsFenApi(this.addFrom)
        console.log(res)
        this.$message.success('添加分类成功')
        this.getCateList()
        this.cateDialog = false
      })
    },
    // 监听对话框的关闭事件  重置表单数据
    addCateClose() {
      this.$refs.addFromRef.resetFields()
      this.selectedKeys = []
      this.addFrom.cat_pid = 0
      this.addFrom.cat_level = 0
    }
  }
}
</script>

<style scoped>
.block{
  position: relative;
  z-index: 1;
}
.el-cascader {
  width: 100%;

}
</style>
